/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: sb.h,v 1.20 2007/02/10 12:40:22 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#include "osdep.h"
#include "db.h"

//
// Custom variables you might want to edit
//
#define DEFAULT_PORT		9899
#define PROGNAME		"sbchat"
#define	VERSION			"0.1.0"
#define PENDING_CONN		10
#define MAX_STRING_LENGTH	1024
#define	MAX_BLOCK_SIZE		4096		// don't try calling write() with blocks bigger than this
#define	MAX_INPUT_CHUNK		1024

#define	SELECT_USEC		100000
#define	PULSE_PER_SECOND	10

//
// Do not edit this unless you know what you are doing...
//

//
// global variables
//
FILE			*logfile;
int			mud_verbosity;
DB_CONN			*dbconn;
pthread_mutex_t		db_mutex;
pthread_mutex_t		desc_mutex;
char			*db_user;
char			*db_pass;
char			*db_name;
char			*db_host;
char			*db_pfx;
int			last_event;
int			time_idle;
int			time_limbo;
int			time_warn;
int			time_timeout;
char			*site_url;
char			*site_domain;

//
// MUD structures
//
typedef struct	descriptor_data	DESCRIPTOR_DATA;
typedef struct	room_data	ROOM_DATA;
typedef struct	character_data	CHARACTER_DATA;
typedef struct	io_buffer	IO_BUFFER;
typedef	struct	cmd_list_item	CMD_LIST_ITEM;
typedef	struct	command_item	COMMAND_ITEM;

struct	descriptor_data {
	DESCRIPTOR_DATA	*next;

	char		*host;		// information about where the person is connecting from
	char		*username;	// their username
	char		*session_id;	// session ID stored in the database

	int		descriptor;	// actual file descriptor pointing to the socket
	int		timer;		// how long the socket has been connected
	int		conn_status;	// Status of the connection
	int		last_event;	// last event seen
	int		user_id;	// user ID stored in the database
	int		room_id;	// which chat room they are talking in
	int		idle_warning;

	char		read_buffer[4 * MAX_INPUT_CHUNK];
	char		error_buffer[4 * MAX_INPUT_CHUNK];

	IO_BUFFER	*input_buffer;
	IO_BUFFER	*output_buffer;
};

struct	io_buffer {
	IO_BUFFER	*next;

	char		*buffer;
};


//
// MUD linked lists
//
extern	DESCRIPTOR_DATA		*descriptor_list;

//
// Different macro definitions
//
#define CON_GET_SID		0
#define CON_CHATTING		1
#define	CON_QUITTING		2
#define CON_QUIT		3

//
// Different connection modes
//
#define	MODE_SLIMCHAT		0
#define	MODE_CLIENT_PULL	1
#define	MODE_SERVER_PUSH	4
#define	MODE_SERVER_PUSH_PROXY	5

//
// Handy macros...
//
#define	MAX(a, b)	((a) > (b) ? (a) : (b))
#define	MIN(a, b)	((a) < (b) ? (a) : (b))

#ifndef FALSE
#define FALSE    0
#endif 

#ifndef TRUE 
#define TRUE     1
#endif 

// functions from main.c
void		shutdown_handler (int sig);
void		alarm_handler (int sig);
void		child_handler (int sig);
void		bootstrap_config(char *filename);
void		bootstrap_signals (void);
void		bootstrap_corelimit(void);
void		shutdown_chat (void);
void		bootstrap_db_config (void);

// functions from logging.c
void		bootstrap_logging (char *log_filename);
int		sb_log(int prio, char *fmt, ...);

// functions from db.c
int		bootstrap_db(void);

// functions from chat.c
void		bootstrap_chat (int port);
void		init_new_descriptor (int sock_fd);
int		descriptor_read (DESCRIPTOR_DATA *desc);
int		descriptor_write (DESCRIPTOR_DATA *desc, char *message);
int		descriptor_reap (DESCRIPTOR_DATA *desc);
int		flush_output (DESCRIPTOR_DATA *desc);
int		poll_from_input_buffer(DESCRIPTOR_DATA *desc);
int		add_to_output_queue(DESCRIPTOR_DATA *desc, char *message);

// functions from primordial.c
void		primordial_soup(DESCRIPTOR_DATA *desc, IO_BUFFER *input_buff);
void		send_greeting_to_desc(DESCRIPTOR_DATA *desc);

// functions from events.c
void		bootstrap_db_lastevent(void);
int		update_session(DESCRIPTOR_DATA *desc);
int		poll_events(DESCRIPTOR_DATA *desc);
void		master_poll_alarms(void);
int		master_poll_events(void);
void		clean_old_sessions();
void		update_all_sessions();
void		error_event_add(DESCRIPTOR_DATA *desc, char *err_msg);
